/*
 * Copyright 2006 - 2007 The MITRE Corporation. All rights reserved.
 * Written by Ayal Spitz
 *
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 2.1
 * (the "License"); you may not use this file except in compliance
 * with the License and the following two conditions:
 * 
 * The US Government will not be charged any license fee and/or royalties 
 * related to this software.
 * 
 * Neither name of The MITRE Corporation; nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * You may obtain a copy of the License at
 *
 *     http://www.gnu.org/copyleft/lesser.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.mitre.fastInfoset;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamResult;

import org.jvnet.fastinfoset.FastInfosetSource;
import org.xml.sax.SAXException;

import com.sun.xml.fastinfoset.sax.SAXDocumentSerializer;

public class FastInfosetUtils {
	public static String toFastInfoSet(String xml)
		throws ParserConfigurationException, SAXException, IOException{
		
		InputStream				xmlIn = null;
		ByteArrayOutputStream	fiOut = null;
		SAXDocumentSerializer	saxDocSerializer = null;
		SAXParserFactory		saxParserFactory = null;
		SAXParser				saxParser = null;
		String					fi = null;
		
		xmlIn = new ByteArrayInputStream(xml.getBytes());
		fiOut = new ByteArrayOutputStream();
		
		saxDocSerializer = new SAXDocumentSerializer();
		saxDocSerializer.setOutputStream(fiOut);
		saxParserFactory = SAXParserFactory.newInstance();
		saxParserFactory.setNamespaceAware(true);
		saxParser = saxParserFactory.newSAXParser();
		saxParser.setProperty("http://xml.org/sax/properties/lexical-handler", saxDocSerializer);
		saxParser.parse(xmlIn, saxDocSerializer);
		
		fi = new String(fiOut.toByteArray());
	
		return fi;
	}

	public static String toXML(String fi)
		throws TransformerFactoryConfigurationError, TransformerException{
	
		InputStream				fiIn = null;
		ByteArrayOutputStream	xmlOut = null;
		String					xml = null;
		Transformer				tx = null;
		FastInfosetSource		fiSrc = null;
		StreamResult			streamResult = null;
		
		fiIn = new ByteArrayInputStream(fi.getBytes());
		xmlOut = new ByteArrayOutputStream();
	
		tx = TransformerFactory.newInstance().newTransformer();
		fiSrc = new FastInfosetSource(fiIn);
		streamResult = new StreamResult(xmlOut);
		tx.transform(fiSrc, streamResult);
	
		xml = new String(xmlOut.toByteArray());
		xml = xml.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "");
		
		return xml;
	}
}
